package com.ybfenpan.controller.admin.wealth;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Admin;
import com.ybfenpan.model.Member;
import com.ybfenpan.model.SysPay;
import com.ybfenpan.service.DailiService;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.SysPayService;
import com.ybfenpan.taglib.annotation.Token;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.CryptUtil;
import com.ybfenpan.utils.MapUtil;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2016/3/25.
 */
@Controller
 
public class SysPayAddController extends BaseController {
    @Resource
    private SysPayService sysPayService;
    @Resource
    private MemberService memberService;
    @Resource
    private DailiService dailiService;
    
    @Resource(name = "taskExecutor")
    private TaskExecutor taskExecutor;
    
    @Token(save=true)
    @RequestMapping("admin/wealth/sysPayAddShow.shtml")
    public String sysPayAddInit() throws Exception {
    	return "admin/wealth/sysPayAdd";
    }
    
   
    @RequestMapping("admin/wealth/sysPayAdd.shtml")
    @ResponseBody
    @Token(remove=true)
    public void add(SysPay sysPay, ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	Map formMap = getParameterMap(request);
    	//新增
    	Admin admin = ContextUtil.getAdmin();
    	String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
    	 
    	String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
    	Boolean result = true;
    	String msg = "";
    	//判断安全密码
    	if(!admin.getTwoPassword().equals(pwd)){
    		setMessage(model,"管理员安全密码不正确。");
    		msg= "管理员安全密码不正确。";
    		result = false;
    	}
    	
    	String loginName = MapUtil.getString(formMap, "loginName");
    	Member member = memberService.executMemberByLoginName(loginName);
    	if(member==null){
    		setMessage(model,"代理商编号不存在。");
    		msg= "代理商编号不存在。";
    		result = false;
    	}
    	
    	 
    	if(result){
    		sysPayService.save(sysPay,member);
    	}
    	
    	HashMap resultformMap = new HashMap();
		resultformMap.put("msg", msg);
		renderSuccess(response, result, resultformMap); 
    	
    	
		
    }
    
    /**
     * loginName检测  ajax
     * @param request
     * @return
     * @throws Exception
     */
	@RequestMapping("admin/wealth/ajaxGetMemberByName.shtml")
	@ResponseBody
	public void ajaxGetMemberByName(HttpServletRequest request, HttpServletResponse response) {


		Map formMap = getParameterMap(request);
		String recommendLoginName = MapUtil.getString(formMap, "loginName");
		Member member = memberService.executMemberByLoginName(recommendLoginName);
		String personName;
		HashMap resultformMap = new HashMap();
		if (member != null) {

			personName = member.getPersonName();
			resultformMap.put("member", member);
			resultformMap.put("personName", personName);


			renderSuccess(response, true, resultformMap);
		} else {

			renderSuccess(response, false, resultformMap);

		}
		
	}
  
}
